package com.example.blackbox.Net;

import android.util.Log;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class NetTool {


    /**
     * 请求json数据
     * @param target 目标地址
     * @return 返回news的json数据
     */
    public static String getJsonString(String target){
        String data="";
        URL url;
        try{
            url=new URL(target);
            HttpURLConnection httpURLConnection=(HttpURLConnection)url.openConnection();
            httpURLConnection.setConnectTimeout(5000);//等待时间
            httpURLConnection.setRequestMethod("POST");//请求方式
            httpURLConnection.setDoOutput(true);//设置输入
            httpURLConnection.setDoInput(true);//设置输入
            httpURLConnection.setUseCaches(false);//设置缓存
            httpURLConnection.setInstanceFollowRedirects(true);
            InputStreamReader in=new InputStreamReader(httpURLConnection.getInputStream());
            BufferedReader buffer=new BufferedReader(in);
            String input="";
            while ((input=buffer.readLine())!=null){
                data =data + input;
            }
            buffer.close();
            httpURLConnection.disconnect();
            return data;
        }catch (Exception e){
            e.printStackTrace();
        }
        return data;
    }

    /**
     * 获取文章信息
     * @param target 目标地址
     * @return 文章信息
     */
    public static String getInfoString(String target){
        String text="";
        URL url;
        try {
            url=new URL(target);
            HttpURLConnection httpURLConnection=(HttpURLConnection)url.openConnection();
            httpURLConnection.setRequestMethod("GET");

            InputStreamReader in=new InputStreamReader(httpURLConnection.getInputStream());
            BufferedReader buffer=new BufferedReader(in);
            String input="";
            while ((input=buffer.readLine())!=null){
                text =text + input+"\n";
            }
            in.close();
            httpURLConnection.disconnect();
            return  text;
        }catch (Exception e){
            e.printStackTrace();
        }
        return text;
    }

    /**
     * 登录验证
     * @param un 用户名
     * @param pwd 密码
     * @return 验证正确返回1，错误为0
     */
    public static String signChecking(String un,String pwd){
        String text="";
        String target="http://10.0.2.2:8080/androids/sign/login.jsp";
        target=target+"?username="+un+"&pwd="+pwd;
        Log.d("test",target);
        URL url;
        try {
            url=new URL(target);
            HttpURLConnection httpURLConnection=(HttpURLConnection)url.openConnection();
            httpURLConnection.setRequestMethod("GET");

            InputStreamReader in=new InputStreamReader(httpURLConnection.getInputStream());
            BufferedReader buffer=new BufferedReader(in);
            String input="";
            while ((input=buffer.readLine())!=null){
                text =text + input;
            }
            in.close();
            httpURLConnection.disconnect();
            return  text;
        }catch (Exception e){
            e.printStackTrace();
        }
        return text;
    }
}
